package com.ruoyi.pack.service.impl;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pack.domain.PackProjectWarehouseOperation;
import com.ruoyi.pack.mapper.PackProjectWarehouseOperationMapper;
import com.ruoyi.pack.service.IPackProjectWarehouseOperationService;

/**
 * 项目仓库操作权限Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class PackProjectWarehouseOperationServiceImpl implements IPackProjectWarehouseOperationService 
{
    private static final Logger log = LoggerFactory.getLogger(PackProjectWarehouseOperationServiceImpl.class);

    @Autowired
    private PackProjectWarehouseOperationMapper packProjectWarehouseOperationMapper;

    /**
     * 查询项目仓库操作权限
     * 
     * @param projectWarehouseId 项目仓库操作权限主键
     * @return 项目仓库操作权限
     */
    @Override
    public PackProjectWarehouseOperation selectPackProjectWarehouseOperationByProjectWarehouseId(Long projectWarehouseId)
    {
        return packProjectWarehouseOperationMapper.selectPackProjectWarehouseOperationByProjectWarehouseId(projectWarehouseId);
    }

    /**
     * 查询项目仓库操作权限列表
     * 
     * @param packProjectWarehouseOperation 项目仓库操作权限
     * @return 项目仓库操作权限
     */
    @Override
    public List<PackProjectWarehouseOperation> selectPackProjectWarehouseOperationList(PackProjectWarehouseOperation packProjectWarehouseOperation)
    {
        return packProjectWarehouseOperationMapper.selectPackProjectWarehouseOperationList(packProjectWarehouseOperation);
    }

    /**
     * 查询项目仓库操作权限列表（用于导出）
     * 
     * @param packProjectWarehouseOperation 项目仓库操作权限
     * @return 项目仓库操作权限
     */
    @Override
    public List<PackProjectWarehouseOperation> selectPackProjectWarehouseOperationListForExport(PackProjectWarehouseOperation packProjectWarehouseOperation)
    {
        return packProjectWarehouseOperationMapper.selectPackProjectWarehouseOperationListForExport(packProjectWarehouseOperation);
    }

    /**
     * 新增项目仓库操作权限
     * 
     * @param packProjectWarehouseOperation 项目仓库操作权限
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPackProjectWarehouseOperation(PackProjectWarehouseOperation packProjectWarehouseOperation)
    {
        // 检查项目仓库权限是否已存在
        if (checkProjectWarehouseExists(packProjectWarehouseOperation.getProjectId(), packProjectWarehouseOperation.getWarehouseId()))
        {
            throw new ServiceException("项目仓库操作权限已存在，无法重复新增");
        }
        
        packProjectWarehouseOperation.setCreateTime(DateUtils.getNowDate());
        return packProjectWarehouseOperationMapper.insertPackProjectWarehouseOperation(packProjectWarehouseOperation);
    }

    /**
     * 修改项目仓库操作权限
     * 
     * @param packProjectWarehouseOperation 项目仓库操作权限
     * @return 结果
     */
    @Override
    @Transactional
    public int updatePackProjectWarehouseOperation(PackProjectWarehouseOperation packProjectWarehouseOperation)
    {
        packProjectWarehouseOperation.setUpdateTime(DateUtils.getNowDate());
        return packProjectWarehouseOperationMapper.updatePackProjectWarehouseOperation(packProjectWarehouseOperation);
    }

    /**
     * 批量删除项目仓库操作权限
     * 
     * @param projectWarehouseIds 需要删除的项目仓库操作权限主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deletePackProjectWarehouseOperationByProjectWarehouseIds(Long[] projectWarehouseIds)
    {
        return packProjectWarehouseOperationMapper.deletePackProjectWarehouseOperationByProjectWarehouseIds(projectWarehouseIds);
    }

    /**
     * 删除项目仓库操作权限信息
     * 
     * @param projectWarehouseId 项目仓库操作权限主键
     * @return 结果
     */
    @Override
    public int deletePackProjectWarehouseOperationByProjectWarehouseId(Long projectWarehouseId)
    {
        return packProjectWarehouseOperationMapper.deletePackProjectWarehouseOperationByProjectWarehouseId(projectWarehouseId);
    }

    /**
     * 根据项目ID查询项目仓库操作权限列表
     * 
     * @param projectId 项目ID
     * @return 项目仓库操作权限集合
     */
    @Override
    public List<PackProjectWarehouseOperation> selectPackProjectWarehouseOperationByProjectId(Long projectId)
    {
        return packProjectWarehouseOperationMapper.selectPackProjectWarehouseOperationByProjectId(projectId);
    }

    /**
     * 根据项目ID删除项目仓库操作权限
     * 
     * @param projectId 项目ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deletePackProjectWarehouseOperationByProjectId(Long projectId)
    {
        return packProjectWarehouseOperationMapper.deletePackProjectWarehouseOperationByProjectId(projectId);
    }

    /**
     * 根据项目ID和仓库ID查询项目仓库操作权限
     * 
     * @param projectId 项目ID
     * @param warehouseId 仓库ID
     * @return 项目仓库操作权限
     */
    @Override
    public PackProjectWarehouseOperation selectPackProjectWarehouseOperationByProjectAndWarehouse(Long projectId, Long warehouseId)
    {
        return packProjectWarehouseOperationMapper.selectPackProjectWarehouseOperationByProjectAndWarehouse(projectId, warehouseId);
    }

    /**
     * 检查项目仓库权限是否存在
     * 
     * @param projectId 项目ID
     * @param warehouseId 仓库ID
     * @return 结果
     */
    @Override
    public boolean checkProjectWarehouseExists(Long projectId, Long warehouseId)
    {
        PackProjectWarehouseOperation operation = packProjectWarehouseOperationMapper.checkProjectWarehouseExists(projectId, warehouseId);
        return StringUtils.isNotNull(operation);
    }

    /**
     * 根据仓库ID查询相关的项目权限配置
     * 
     * @param warehouseId 仓库ID
     * @return 项目仓库操作权限集合
     */
    @Override
    public List<PackProjectWarehouseOperation> selectPackProjectWarehouseOperationByWarehouseId(Long warehouseId)
    {
        return packProjectWarehouseOperationMapper.selectPackProjectWarehouseOperationByWarehouseId(warehouseId);
    }

    /**
     * 保存或更新项目仓库操作权限
     * 
     * @param packProjectWarehouseOperation 项目仓库操作权限
     * @return 结果
     */
    @Override
    @Transactional
    public int saveOrUpdateProjectWarehouseOperation(PackProjectWarehouseOperation packProjectWarehouseOperation)
    {
        PackProjectWarehouseOperation existing = selectPackProjectWarehouseOperationByProjectAndWarehouse(
            packProjectWarehouseOperation.getProjectId(), 
            packProjectWarehouseOperation.getWarehouseId()
        );
        
        if (StringUtils.isNotNull(existing))
        {
            // 存在则更新
            packProjectWarehouseOperation.setProjectWarehouseId(existing.getProjectWarehouseId());
            return updatePackProjectWarehouseOperation(packProjectWarehouseOperation);
        }
        else
        {
            // 不存在则新增
            return insertPackProjectWarehouseOperation(packProjectWarehouseOperation);
        }
    }

    /**
     * 批量设置项目仓库操作权限
     * 
     * @param projectId 项目ID
     * @param operationList 操作权限列表
     * @return 结果
     */
    @Override
    @Transactional
    public int batchSaveProjectWarehouseOperation(Long projectId, List<PackProjectWarehouseOperation> operationList)
    {
        if (StringUtils.isNull(operationList) || operationList.size() == 0)
        {
            throw new ServiceException("批量设置项目仓库操作权限数据不能为空！");
        }
        
        int count = 0;
        for (PackProjectWarehouseOperation operation : operationList)
        {
            operation.setProjectId(projectId);
            count += saveOrUpdateProjectWarehouseOperation(operation);
        }
        return count;
    }

    /**
     * 验证操作权限
     * 
     * @param projectId 项目ID
     * @param warehouseId 仓库ID
     * @param operationType 操作类型
     * @return 是否有权限
     */
    @Override
    public boolean validateOperation(Long projectId, Long warehouseId, String operationType)
    {
        PackProjectWarehouseOperation operation = selectPackProjectWarehouseOperationByProjectAndWarehouse(projectId, warehouseId);
        if (StringUtils.isNull(operation))
        {
            return false;
        }
        
        switch (operationType)
        {
            case "FULL_INBOUND":
                return "1".equals(operation.getAllowFullInbound());
            case "FULL_OUTBOUND":
                return "1".equals(operation.getAllowFullOutbound());
            case "EMPTY_INBOUND":
                return "1".equals(operation.getAllowEmptyInbound());
            case "EMPTY_OUTBOUND":
                return "1".equals(operation.getAllowEmptyOutbound());
            case "PACKING":
                return "1".equals(operation.getAllowPacking());
            case "UNPACKING":
                return "1".equals(operation.getAllowUnpacking());
            case "FOLDING":
                return "1".equals(operation.getAllowFolding());
            case "CLEANING":
                return "1".equals(operation.getAllowCleaning());
            case "REPAIR":
                return "1".equals(operation.getAllowRepair());
            default:
                return false;
        }
    }
} 